package com.config;

import com.alibaba.fastjson.JSONObject;
import com.utils.CommonUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/5/1 0:13
 * @since JDK 1.8
 */
@Aspect
@Component
@Order(4)
@Slf4j
public class ModelValueAopConfig {

    @Pointcut("@annotation(com.config.ModelMapValue)")
    private void pointCut(){};

    @Around("pointCut()")
    public Object aroundJobConfig(ProceedingJoinPoint point) throws Throwable{
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response=attributes.getResponse();
        Object result=null;
        String proName=null;
        try {
            Class<?> cls=point.getTarget().getClass();
            String method=point.getSignature().getName();
            Method method1= CommonUtils.getMethod(method,cls);
            ModelMapValue modelMapValue=method1.getDeclaredAnnotation(ModelMapValue.class);
            proName=modelMapValue.value();
            System.out.println(proName+"  proName");
            String path=CommonUtils.getFilePathBykey(proName);
            File file=new File(path);
            Configuration configuration= ContextUtils.getBeanObj("common-freemarker");
            configuration.setDirectoryForTemplateLoading(file);
            result=point.proceed();
            if (result==null){
                return null;
            }
            Template template = configuration.getTemplate(result+".html");
            response.setContentType("text/html; charset=" + template.getEncoding());
            // 准备输出， 使用模板的编码作为本页的charset

            //获取传入目标方法的参数
            Map<String, Object> stringObjectMap=new HashMap<>();
            Object[] args = point.getArgs();
            for (int i = 0; i < args.length; i++) {
                if(args[i] instanceof Map){
                    stringObjectMap.putAll((Map)args[i]);
                }
            }
            String resPath=CommonUtils.getFilePathBykey("res");
            stringObjectMap.put("res",resPath);
            PrintWriter out = response.getWriter();
            // 在模板中加入动态数据
            template.process(stringObjectMap, out);
        }catch (Exception e){
            log.info("出现异常");
        }
        return result;
    }



}
